package net.huashitong.Indicators.service;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sirdc.modules.utils.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;

import net.huashitong.Indicators.databean.IndicatorBreakdownDatabean;
import net.huashitong.Indicators.entity.IndicatorBreakdown;
import net.huashitong.Indicators.filter.IndicatorBreakdownFilter;
import net.huashitong.Indicators.utils.DataUtils;
import net.huashitong.Indicators.utils.DateForIndUtils;

@Service
public class IndicatorSecondService {
	@Autowired
	private TertiaryIndustryService industryService;
	@Autowired
	private IndicatorBreakdownService breakdownService;
	@Autowired
	private CompletionClassificationService classificationService;
	//获取排名数据
	public List<IndicatorBreakdownDatabean> queryCountyRank(IndicatorBreakdownFilter filter){
		List<IndicatorBreakdownDatabean> databeans = new ArrayList<IndicatorBreakdownDatabean>(13);
		List<IndicatorBreakdown> lists = breakdownService.queryCountyRank(filter);
		if(lists.size()<3){
			return null;
		}
		DecimalFormat df=new DecimalFormat("##.##");
		for(IndicatorBreakdown indicatorBreakdown : lists){
			IndicatorBreakdownDatabean databean = new IndicatorBreakdownDatabean();
			databean.setArea(indicatorBreakdown.getArea());
			databean.setRatio(df.format(Double.parseDouble(indicatorBreakdown.getRatio())));
			if("0".equals(indicatorBreakdown.getCumulative())||"".equals(indicatorBreakdown.getCumulative())){
				databean.setCumulative("0");
			}else{
				String st=df.format(Double.parseDouble(indicatorBreakdown.getCumulative())/10000);
				databean.setCumulative(st);
			}
			databeans.add(databean);
		}
		return databeans;
	 }
	
	// 获取当月同比增长情况
	public List<IndicatorBreakdownDatabean> queryMonthDevelopment(IndicatorBreakdownFilter filter) throws ParseException {
		List<IndicatorBreakdownDatabean> breakdownDatabean2s = new ArrayList<IndicatorBreakdownDatabean>();
		List<IndicatorBreakdown> lists2 = breakdownService.queryMonthDevelopmentSituation(filter);
		
		IndicatorBreakdownDatabean databean = new IndicatorBreakdownDatabean();
		String lastDateVal = "";
		String DateVal = "";
		for (IndicatorBreakdown indicatorBreakdown : lists2) {
			if(StringUtils.equals(indicatorBreakdown.getCurrentMonth(), DateForIndUtils.getNextMonthDate(filter.getCurrentMonth(), -1))){
				lastDateVal = indicatorBreakdown.getCumulative();
			}else {
				DateVal = indicatorBreakdown.getCumulative();
				databean.setRatio(indicatorBreakdown.getRatio());
				databean.setCumulative(indicatorBreakdown.getCumulative());
			}
			
		}
		breakdownDatabean2s.add(databean);
		return breakdownDatabean2s;
	}
	
	
	// 当月同比增长情况
	public List<Map<String, Object>> queryMonthAbsolute(IndicatorBreakdownFilter filter) throws ParseException {
		IndicatorBreakdown breakdown = breakdownService.queryMonthValue(filter);
		if(ObjectUtils.isBlank(breakdown)){
			return null;
		}
		filter.setDate(DateForIndUtils.getNextYearDate(filter.getDate().substring(0,4),0));
		List<IndicatorBreakdown> breakdowns = breakdownService.queryMonthAbsolute(filter);
		List<IndicatorBreakdownDatabean> breakdownDatabeans = new ArrayList<IndicatorBreakdownDatabean>();
		breakdownDatabeans = DataUtils.detailData(breakdowns,breakdownDatabeans);
		filter.setDate(DateForIndUtils.getNextYearDate(filter.getDate(),-1));
		List<IndicatorBreakdown> breakdownSs = breakdownService.queryMonthAbsolute(filter);
		DecimalFormat df=new DecimalFormat("#.##");
		breakdownDatabeans = DataUtils.detailData(breakdownSs,breakdownDatabeans);
		List<IndicatorBreakdownDatabean> breakdownDatabeans2 = new ArrayList<IndicatorBreakdownDatabean>();
		List<IndicatorBreakdownDatabean> breakdownDatabeans3 = new ArrayList<IndicatorBreakdownDatabean>();
		for (IndicatorBreakdownDatabean breakdownDatabean : breakdownDatabeans) {
			for (IndicatorBreakdownDatabean databean2 : breakdownDatabeans) {
				if(databean2.getCurrentMonth().contains(DateForIndUtils.getNextMonthDate(breakdownDatabean.getCurrentMonth(),-12))){
					Double double5 = Double.parseDouble(breakdownDatabean.getSeasonsComparison());
					Double double6 = Double.parseDouble(databean2.getSeasonsComparison());
					Double double7 = (double5-double6)/double6*100;
					if(double7 != 0){
						String st=df.format(double7);
						breakdownDatabean.setConcurrentGrowth(st);
					}
				}
			}
			breakdownDatabean.setSeasonsComparison(df.format(Double.parseDouble(breakdownDatabean.getSeasonsComparison())/10000));
			if(breakdownDatabean.getCurrentMonth().contains("2016")){
				breakdownDatabean.setCurrentMonth(breakdownDatabean.getCurrentMonth().substring(4));
				breakdownDatabeans2.add(breakdownDatabean);
			}else {
				breakdownDatabean.setCurrentMonth(breakdownDatabean.getCurrentMonth().substring(4));
				breakdownDatabeans3.add(breakdownDatabean);
			}
		}
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		Map<String, Object> map2 = new HashMap<String, Object>();
		Collections.reverse(breakdownDatabeans2);
		Collections.reverse(breakdownDatabeans3);
		map2.put("lastYear", breakdownDatabeans2);
		map2.put("years", breakdownDatabeans3);
		list.add(map2);
		return list;
	}
	
	
	public List<Map<String, Object>> queryLJAbsolute(
			IndicatorBreakdownFilter filter) throws ParseException {
		IndicatorBreakdown breakdown = breakdownService.queryMonthValue(filter);
		if(ObjectUtils.isBlank(breakdown)){
			return null;
		}
		filter.setDate(DateForIndUtils.getNextYearDate(filter.getDate().substring(0,4),0));
//		filter.setDate(DateForIndUtils.getNextYearDate(date.substring(0,4),0));
		List<IndicatorBreakdown> breakdown3s = breakdownService.queryMonthAbsolute(filter);
		if(ObjectUtils.isBlank(breakdown3s)){
			return null;
		}
		DecimalFormat df=new DecimalFormat("#.##");
		List<IndicatorBreakdownDatabean> breakdownDatabean3s = new ArrayList<IndicatorBreakdownDatabean>();
		breakdownDatabean3s = DataUtils.detailcumulativeData(breakdown3s,breakdownDatabean3s);
		filter.setDate(DateForIndUtils.getNextYearDate(filter.getDate(),-1));
		List<IndicatorBreakdown> breakdown3Ss = breakdownService.queryMonthAbsolute(filter);
		if(ObjectUtils.isBlank(breakdown3Ss)){
			return null;
		}
		breakdownDatabean3s = DataUtils.detailcumulativeData(breakdown3Ss,breakdownDatabean3s);
		Map<String, Object> map = new HashMap<String, Object>();
		List<IndicatorBreakdownDatabean> breakdownDatabean4s = new ArrayList<IndicatorBreakdownDatabean>();
		List<IndicatorBreakdownDatabean> breakdownDatabeans4 = new ArrayList<IndicatorBreakdownDatabean>();
		for (IndicatorBreakdownDatabean indicatorBreakdown : breakdownDatabean3s) {
			indicatorBreakdown.setCumulative(df.format(Double.parseDouble(indicatorBreakdown.getCumulative())/10000));
			if(indicatorBreakdown.getCurrentMonth().contains("2016")){
				indicatorBreakdown.setCurrentMonth(indicatorBreakdown.getCurrentMonth().substring(4));
				breakdownDatabean4s.add(indicatorBreakdown);
			}else {
				indicatorBreakdown.setCurrentMonth(indicatorBreakdown.getCurrentMonth().substring(4));
				breakdownDatabeans4.add(indicatorBreakdown);
			}
		}
		List<Map<String, Object>> list32 = new ArrayList<Map<String,Object>>();
		Map<String, Object> map32 = new HashMap<String, Object>();
		Collections.reverse(breakdownDatabean4s);
		Collections.reverse(breakdownDatabeans4);
		map32.put("years",breakdownDatabeans4);
		map32.put("lastYear", breakdownDatabean4s);
		String st= "";
		if(StringUtils.isNotBlank(breakdown3s.get(0).getExpectedTarget())){
			st=df.format(Double.parseDouble(breakdown3s.get(0).getExpectedTarget())/10000);
		}
		map32.put("expectedTarget", st);
		list32.add(map32);
		return list32;
	}
	
	
	/******************按季************************/
	/**
	 *  GDP当季同比增长情况
	 * @param filter
	 * @return
	 * @throws ParseException 
	 */
	public List<IndicatorBreakdownDatabean> queryDevelopmentSituation(
			IndicatorBreakdownFilter filter) throws ParseException {
		List<IndicatorBreakdownDatabean> breakdownDatabean2s = new ArrayList<IndicatorBreakdownDatabean>();
		List<IndicatorBreakdown> lists2 = breakdownService.queryMonthDevelopmentSituation(filter);
		//校验
		if(lists2.size()<2){
			return null;
		}
		
		String lastDateVal = "";
		String DateVal = "";
		IndicatorBreakdownDatabean databean = new IndicatorBreakdownDatabean();
		for (IndicatorBreakdown indicatorBreakdown : lists2) {
			
			if(StringUtils.equals(indicatorBreakdown.getCurrentMonth(), DateForIndUtils.getNextMonthDate(filter.getCurrentMonth(), -1))){
				lastDateVal = indicatorBreakdown.getCumulative();
			}else {
				DateVal = indicatorBreakdown.getCumulative();
				databean.setRatio(indicatorBreakdown.getRatio());
				databean.setCountyIncrease(indicatorBreakdown.getExpectedTarget());
				databean.setCityIncrease(indicatorBreakdown.getCountyIncrease());
			}
			
		}
		breakdownDatabean2s.add(databean);
		return breakdownDatabean2s;
	}
	//GDP累计同比增长情况
	public List<Map<String, Object>> queryCumulativeGrowth(
			IndicatorBreakdownFilter filter) throws ParseException {
		IndicatorBreakdown breakdown = breakdownService.queryMonthValue(filter);
		if(ObjectUtils.isBlank(breakdown)){
			return null;
		}
		filter.setDate(DateForIndUtils.getNextYearDate(filter.getDate().substring(0,4),0));
		List<IndicatorBreakdown> breakdowns = breakdownService.queryAbsoluteValue(filter);
		List<IndicatorBreakdownDatabean> breakdownDatabeans = new ArrayList<IndicatorBreakdownDatabean>();
		breakdownDatabeans = DataUtils.detailcumulativeData(breakdowns,breakdownDatabeans);
		filter.setDate(DateForIndUtils.getNextYearDate(filter.getDate(),-1));
		List<IndicatorBreakdown> breakdownSs = breakdownService.queryAbsoluteValue(filter);
		breakdownDatabeans = DataUtils.detailcumulativeData(breakdownSs,breakdownDatabeans);
		DecimalFormat df=new DecimalFormat("#.##");
		List<IndicatorBreakdownDatabean> breakdownDatabean4s = new ArrayList<IndicatorBreakdownDatabean>();
		List<IndicatorBreakdownDatabean> breakdownDatabeans4 = new ArrayList<IndicatorBreakdownDatabean>();
		for (IndicatorBreakdownDatabean indicatorBreakdown : breakdownDatabeans) {
			if(ObjectUtils.isNotBlank(indicatorBreakdown.getCumulative())){
			indicatorBreakdown.setCumulative(df.format(Double.parseDouble(indicatorBreakdown.getCumulative())/10000));
			}else{
				indicatorBreakdown.setCumulative("0");
			}
			if(indicatorBreakdown.getCurrentMonth().contains(filter.getDate().substring(0,4))){
				indicatorBreakdown.setCurrentMonth(indicatorBreakdown.getCurrentMonth().substring(4));
				breakdownDatabean4s.add(indicatorBreakdown);
			}else {
				indicatorBreakdown.setCurrentMonth(indicatorBreakdown.getCurrentMonth().substring(4));
				breakdownDatabeans4.add(indicatorBreakdown);
			}
		}
		List<Map<String, Object>> list32 = new ArrayList<Map<String,Object>>();
		Map<String, Object> map32 = new HashMap<String, Object>();
		Collections.reverse(breakdownDatabean4s);
		Collections.reverse(breakdownDatabeans4);
		map32.put("years",breakdownDatabeans4);
		map32.put("lastYear", breakdownDatabean4s);
		String st=//df.format(Double.parseDouble(breakdowns.get(0).getExpectedTarget())/10000);
				StringUtils.isBlank(breakdowns.get(0).getExpectedTarget())?"0": NumberUtils.keepPrecision(Double.parseDouble(breakdowns.get(0).getExpectedTarget())/10000,2);
		map32.put("expectedTarget", st);
		list32.add(map32);
		return list32;
	}
}
